package com.wave.web.controller.system;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.wave.common.annotation.Log;
import com.wave.common.annotation.RepeatSubmit;
import com.wave.common.core.controller.BaseController;
import com.wave.common.core.domain.PageQuery;
import com.wave.common.core.domain.R;
import com.wave.common.core.page.TableDataInfo;
import com.wave.common.core.validate.AddGroup;
import com.wave.common.core.validate.EditGroup;
import com.wave.common.enums.BusinessType;
import com.wave.common.utils.poi.ExcelUtil;
import com.wave.system.domain.bo.SysSettingBo;
import com.wave.system.domain.vo.SysSettingVo;
import com.wave.system.service.ISysSettingService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 系统设置
 *
 * @author wave
 * @date 2024-10-15
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/setting")
public class SysSettingController extends BaseController {

    private final ISysSettingService iSysSettingService;

    /**
     * 查询系统设置列表
     */
    @SaCheckPermission("system:setting:list")
    @GetMapping("/list")
    public TableDataInfo<SysSettingVo> list(SysSettingBo bo, PageQuery pageQuery) {
        return iSysSettingService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出系统设置列表
     */
    @SaCheckPermission("system:setting:export")
    @Log(title = "系统设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysSettingBo bo, HttpServletResponse response) {
        List<SysSettingVo> list = iSysSettingService.queryList(bo);
        ExcelUtil.exportExcel(list, "系统设置", SysSettingVo.class, response);
    }

    /**
     * 获取系统设置详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:setting:query")
    @GetMapping("/{id}")
    public R<SysSettingVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long id) {
        return R.ok(iSysSettingService.queryById(id));
    }

    /**
     * 新增系统设置
     */
    @SaCheckPermission("system:setting:add")
    @Log(title = "系统设置", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysSettingBo bo) {
        return toAjax(iSysSettingService.insertByBo(bo));
    }

    /**
     * 修改系统设置
     */
    @SaCheckPermission("system:setting:edit")
    @Log(title = "系统设置", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysSettingBo bo) {
        return toAjax(iSysSettingService.updateByBo(bo));
    }

    /**
     * 删除系统设置
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:setting:remove")
    @Log(title = "系统设置", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iSysSettingService.deleteWithValidByIds(Arrays.asList(ids), true));
    }


    /**
     * 校验地址是否可用
     */
    @GetMapping("/checkAddress/{address}")
    @Log(title = "校验地址是否可用", businessType = BusinessType.OTHER)
    public R<Void> checkAddress(@NotEmpty(message = "地址不能为空") @PathVariable String address) {
        boolean result = iSysSettingService.checkAddress(address);
        return result ? R.ok("验证成功") : R.fail("验证失败");
    }
}
